#include 	"main.h"
#include 	"system.h"

void SendDataToCollectBoard(uint8 *p, uint8 length)
{
    ResppackRece = 0;
    HAL_UART_Transmit(&CommunicationUart,p,length, 0xFFFF);
}

uint8 SumCheck(uint8 *p, uint16 length)
{
    uint16 sum = 0;
    uint8 *point = p;
    uint16 i = 0;
    uint16 crc = 0;
    ��У���
    for (i = 0; i < length - 2; i++)
    {
        sum += (*(point + i));
    }
    crc = (((uint16) * (p + length - 1)) << 8) | ((uint16) * (p + length - 2));��ͨ��
    if (sum == crc)
    {
        return 1;
    }
    �鲻ͨ��
    else
    {
        return 0;
    }
}
uint16 SumCalculate(uint8 *p, uint8 length)
{
    uint16 sum = 0;
    uint8 *point = p;
    uint8 i = 0;
    ��У���
    for (i = 0; i < length - 2; i++)
    {
        sum += (*(point + i));
    }
    return sum;
}


#if 0
uint8 SendCollectedValToDisplayBoard(CollectedDataType datatype,  CollectedData *result)
{
    uint8 i = 0;
		uint16  crc=0;
    uint8 sendbuffer[13] ={0x55,0XAA,0X01,  0,0,0,0,0,0,0,0,  0,0};

    if(datatype==CurrentValue)
    {

		sendbuffer[2]=CurrentValue;

		sendbuffer[3]=(uint8)(result->CurData.aVal>>8);
		sendbuffer[4]=(uint8)(result->CurData.aVal&0X00FF);
		
		sendbuffer[5]=(uint8)(result->CurData.bVal>>8);
		sendbuffer[6]=(uint8)(result->CurData.bVal&0X00FF);
		
		sendbuffer[7]=(uint8)(result->CurData.cVal>>8);
		sendbuffer[8]=(uint8)(result->CurData.cVal&0X00FF);
		
		sendbuffer[9]=(uint8)(result->CurData.rVal>>8);
		sendbuffer[10]=(uint8)(result->CurData.rVal&0X00FF);

	}
	else if(datatype==TemperatureValue)
	{
		sendbuffer[2]=TemperatureValue;

		
		sendbuffer[3]=(uint8)(result->TemData.aVal>>8);
		sendbuffer[4]=(uint8)(result->TemData.aVal&0X00FF);
		
		sendbuffer[5]=(uint8)(result->TemData.bVal>>8);
		sendbuffer[6]=(uint8)(result->TemData.bVal&0X00FF);
		
		sendbuffer[7]=(uint8)(result->TemData.cVal>>8);
		sendbuffer[8]=(uint8)(result->TemData.cVal&0X00FF);

		sendbuffer[9]=(uint8)(result->TemData.sVal>>8);
		sendbuffer[10]=(uint8)(result->TemData.sVal&0X00FF);
	}
	else if(datatype==VoltageValue)
	{
		sendbuffer[2]=VoltageValue;

		
		sendbuffer[3]=(uint8)(result->VolData.aVal>>8);
		sendbuffer[4]=(uint8)(result->VolData.aVal&0X00FF);
		
		sendbuffer[5]=(uint8)(result->VolData.bVal>>8);
		sendbuffer[6]=(uint8)(result->VolData.bVal&0X00FF);
		
		sendbuffer[7]=(uint8)(result->VolData.cVal>>8);
		sendbuffer[8]=(uint8)(result->VolData.cVal&0X00FF);
		
	}
	
	crc=SumCalculate(sendbuffer,13);
	sendbuffer[11]=(uint8)(crc>>8);
	sendbuffer[12]=(uint8)(crc&0X00FF);
	
	printf("\n");
	for(i=0;i<13;i++)
	{
		printf("%02x:",sendbuffer[i]);
	}
	printf("\n");
	
    while (ResppackRece == 0)
    {
        if ((i % 11 == 0) && (i > 0))
        {
            SendDataToCollectBoard(sendbuffer,sizeof(sendbuffer));
        }
        HAL_Delay(30);
        ++i;
        if (i >= 11 * 3)
        {
            return ERR_FAIL;
        }
    }
    ResppackRece = 0;
    if (SumCheck(CollectBoard_ReceData->buf,13) == 0)
    {
        return ERR_FAIL;
    }

    if (CollectBoard_ReceData->buf[2] == datatype)
    {
        return ERR_SUCCESS;
    }

    return ERR_FAIL;
}
#endif
CollectedDataType  GetCollectedResult(CollectedData *result)
{
	uint8 	*Buffer=NULL,i=0;
	uint8	head[3]={0x55,0xaa,0};

	Buffer=strstr(CollectBoard_ReceData->buf,head);
	if(Buffer==NULL)
	{
		printf("Not CollectedResult\n");
		return ErrorType;
	}
   	if(!(Buffer[0]==0x55&&Buffer[1]==0xaa))
   	{
		printf("not CollectedResult\n");	
		return ErrorType; 
	}
	if(SumCheck(Buffer,35)==0)
	{
		
	}
	else
	{
		printf("crc err\n");	
		return ErrorType;
	}
    if(Buffer[2]==CurrentValue)
    {


		/*printf("\n");
		for(i=0;i<22;i++)
		{
			printf("%02x:",Buffer[i]);
		}
		printf("\n");*/
	

	
		result->CurData.aVal=Buffer[3]<<8|Buffer[4];
		result->CurData.bVal=Buffer[5]<<8|Buffer[6];
		result->CurData.cVal=Buffer[7]<<8|Buffer[8];
		result->CurData.rVal=Buffer[9]<<8|Buffer[10];
		
		
		
		
		return CurrentValue;
		
	}
	else if(Buffer[2]==VoltageValue)
	{
		result->VolData.aVal=Buffer[3]<<8|Buffer[4];
		result->VolData.bVal=Buffer[5]<<8|Buffer[6];
		result->VolData.cVal=Buffer[7]<<8|Buffer[8];

		
		
		
		return VoltageValue;
	}
	else if(Buffer[2]==TemperatureValue)
	{
		result->TemData.aVal=Buffer[3]<<8|Buffer[4];
		result->TemData.bVal=Buffer[5]<<8|Buffer[6];
		result->TemData.cVal=Buffer[7]<<8|Buffer[8];
		result->TemData.sVal=Buffer[9]<<8|Buffer[10];

		
		
		return TemperatureValue;
	}
	else if(Buffer[2]==IMEIValue)
	{
		memcpy(result->IMEI,Buffer+3,IMEI_Len);
		result->IMEI[IMEI_Len]=0;
		printf("--->>IMEI=%s\n",result->IMEI);
		result->SignalStrength=Buffer[31];
		result->OnLineFlag=Buffer[32];			
		result->ModuleType=NBModule;
		return IMEIValue;
	}
	else if(Buffer[2]==DEVEUI)
	{
		memcpy(result->DEVEUI,Buffer+3,DEVEUI_Len);
		result->DEVEUI[DEVEUI_Len]=0;
		printf("--->>DEVEUI=%s\n",result->DEVEUI);
		result->SignalStrength=Buffer[31];
		result->OnLineFlag=Buffer[32];			
		result->ModuleType=LoraModule;
		return DEVEUI;
	}	
	else if(Buffer[2]==ThresholdValue) 
	{
		printf("ThresholdValue\n");
		
		SysPara.thresholdVal.CurThreshold_a=Buffer[3]<<8|Buffer[4];
		SysPara.thresholdVal.CurThreshold_b=Buffer[5]<<8|Buffer[6];
		SysPara.thresholdVal.CurThreshold_c=Buffer[7]<<8|Buffer[8];
		SysPara.thresholdVal.CurThreshold_r=Buffer[9]<<8|Buffer[10];


		SysPara.thresholdVal.Vol_OverThreshold_a=Buffer[11]<<8|Buffer[12];
		SysPara.thresholdVal.Vol_OverThreshold_b=Buffer[13]<<8|Buffer[14];
		SysPara.thresholdVal.Vol_OverThreshold_c=Buffer[15]<<8|Buffer[16];
	
		SysPara.thresholdVal.Vol_BelowThreshold_a=Buffer[17]<<8|Buffer[18];
		SysPara.thresholdVal.Vol_BelowThreshold_b=Buffer[19]<<8|Buffer[20];
		SysPara.thresholdVal.Vol_BelowThreshold_c=Buffer[21]<<8|Buffer[22];


		SysPara.thresholdVal.TemThreshold_a=Buffer[23]<<8|Buffer[24];
		SysPara.thresholdVal.TemThreshold_b=Buffer[25]<<8|Buffer[26];
		SysPara.thresholdVal.TemThreshold_c=Buffer[27]<<8|Buffer[28];
		SysPara.thresholdVal.TemThreshold_s=Buffer[29]<<8|Buffer[30];

		printf("--->>>����ֵ\nC=%d ",SysPara.thresholdVal.CurThreshold_a);
		printf("%d ",SysPara.thresholdVal.CurThreshold_b);	
		printf("%d ",SysPara.thresholdVal.CurThreshold_c);	
		printf("%d\n",SysPara.thresholdVal.CurThreshold_r); 		
		
		printf("V=%d ",SysPara.thresholdVal.Vol_OverThreshold_a);
		  printf("%d ",SysPara.thresholdVal.Vol_OverThreshold_b);	
		 printf("%d\n",SysPara.thresholdVal.Vol_OverThreshold_c);	
		
		  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_a);
		  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_b);
		 printf("%d\n",SysPara.thresholdVal.Vol_BelowThreshold_c);
		
		printf("T=%d ",SysPara.thresholdVal.TemThreshold_a);
		printf("%d ",SysPara.thresholdVal.TemThreshold_b);
		printf("%d ",SysPara.thresholdVal.TemThreshold_c);
		printf("%d\n",SysPara.thresholdVal.TemThreshold_s);	

		printf("<<<---->>save data111\n");
		SysPara.thresholdVal_UsedDefault=Not_UsedDefaultValue;
		SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));	
		
		SendDataToCollectBoard(Cmd4SuccessAck,strlen(Cmd4SuccessAck));
	}
	else if(Buffer[2]==ChannleEnableValue) 
	{
		SysPara.ChannleStatus.CurChannle_a=Buffer[3];
		SysPara.ChannleStatus.CurChannle_b=Buffer[4];
		SysPara.ChannleStatus.CurChannle_c=Buffer[5];
		SysPara.ChannleStatus.CurChannle_r=Buffer[6];

		SysPara.ChannleStatus.VolChannle_a=Buffer[7];
		SysPara.ChannleStatus.VolChannle_b=Buffer[8];
		SysPara.ChannleStatus.VolChannle_c=Buffer[9];

		SysPara.ChannleStatus.TemChannle_a=Buffer[10];
		SysPara.ChannleStatus.TemChannle_b=Buffer[11];
		SysPara.ChannleStatus.TemChannle_c=Buffer[12];
		SysPara.ChannleStatus.TemChannle_s=Buffer[13];	

		printf("--->>>ͨ������\nC=%02x ",SysPara.ChannleStatus.CurChannle_a);
		printf("%02x ",SysPara.ChannleStatus.CurChannle_b);
		printf("%02x ",SysPara.ChannleStatus.CurChannle_c);
		printf("%02x\n",SysPara.ChannleStatus.CurChannle_r);

		printf("V=%02x ",SysPara.ChannleStatus.VolChannle_a);
		printf("%02x ",SysPara.ChannleStatus.VolChannle_b);
		printf("%02x\n",SysPara.ChannleStatus.VolChannle_c);

		printf("T=%02x ",SysPara.ChannleStatus.TemChannle_a);
		printf("%02x ",SysPara.ChannleStatus.TemChannle_b);
		printf("%02x ",SysPara.ChannleStatus.TemChannle_c);
		printf("%02x\n",SysPara.ChannleStatus.TemChannle_s);
		
		
		printf("<<<---->>save data222\n");
		SysPara.ChannleStatus_UsedDefault=Not_UsedDefaultValue;
		SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));	
		
		SendDataToCollectBoard(Cmd5SuccessAck,strlen(Cmd5SuccessAck));
	}
	else
	{
		return ErrorType;
	}
}